package com.mall.user.dto;

import com.mall.commons.result.AbstractRequest;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.Data;

import java.util.regex.Pattern;

/**
 * @program: cs-mall_35th
 * @description: 验证用户的username、phone、email的唯一性
 * @author: Weiwei Liu
 * @create: 2021-12-11 20:47
 **/
@Data
public class UniqueKeyMemberRequest extends AbstractRequest {

    private String email;
    private String username;


    @Override
    public void requestCheck() {

        if (email == null || username == null) {
            // 用户注册信息为null
            throw new ValidateException(
                    SysRetCodeConstants.REQUISITE_PARAMETER_NOT_EXIST.getCode(),
                    SysRetCodeConstants.REQUISITE_PARAMETER_NOT_EXIST.getMessage());

        }

        if (!emailCheck(email)) {
            // 邮箱格式不合法
            throw new ValidateException(
                    SysRetCodeConstants.USER_REGISTER_EMAIL_INVALID.getCode(),
                    SysRetCodeConstants.USER_REGISTER_EMAIL_INVALID.getMessage());
        }


    }

    private boolean emailCheck(String email) {

        String pattern = "^[0-9a-z]+\\w*@([0-9a-z]+\\.)+[0-9a-z]+$";
        return Pattern.matches(pattern, email);
    }
}
